﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABCLogistic_System_V2._0.GUI;
using ABCLogistic_System_V2._0.BL;
using ABCLogistic_System_V2._0.DA;

namespace ABCLogistic_System_V2._0
{
    public partial class frmLogin : Form
    {
        ABCLogisticsEntities context = new ABCLogisticsEntities();
        NHANVIEN nhanvien = new NHANVIEN();
        TAIKHOAN taikhoan = new TAIKHOAN();

        public frmLogin()
        {
            InitializeComponent();
        }

        #region Xu ly cac truong hop thoat chuong trinh
        /// <summary>
        /// xet du lieu trong cac textbox la rong de dong cua so dang nhap
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (txtusername.Text == "" || txtpassword.Text == "")
            {
                this.Close();
            }
        }

        /// <summary>
        /// xet du lieu trong cac textbox co du lieu thi thong bao va dong hoac dang nhap
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
             if (txtusername.Text != "" || txtpassword.Text != "")
            {
                DialogResult result = MessageBox.Show("Bạn muốn thoát?", "Thông báo!", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }
        #endregion

        #region Xu ly dang nhap

        /// <summary>
        /// xu ly nut dang nhap
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string strtendangnhap = txtusername.Text;
            string strmatkhau = txtpassword.Text;

            if (strtendangnhap == "" || strmatkhau == "") MessageBox.Show("Thiếu tên đăng nhập hoặc mật khẩu!");
            else
            {
                if (strtendangnhap != null)
                {
                    TAIKHOAN tendangnhap = context.TAIKHOANs.Where(p => p.TENDANGNHAP == strtendangnhap).FirstOrDefault();
                    if (tendangnhap != null)
                    {
                        if (tendangnhap.MATKHAU == strmatkhau)
                        {
                            var hovatendangnhap = from p in context.TAIKHOANs
                                                  where tendangnhap.TENDANGNHAP == nhanvien.MANV
                                                  select p.NHANVIEN.MANV;


                            string sHovaten = hovatendangnhap.ToString();
                            //NV.hoten = sHovaten;
                            frmMainInterface giaodienchinh = new frmMainInterface(sHovaten);
                            giaodienchinh.Show();
                            this.Hide();
                        }
                        else MessageBox.Show("Mật khẩu sai!");
                    }
                }
            }
            
        }

        #endregion
    }
}
